[Pwnable.kr] fd

題目

Mommy! what is a file descriptor in Linux?

解法

首先利用 SSH 進入主機後,可以看到有三個檔案個別是 fdfd.cflag

1
2
3
-r-sr-x--- 1 fd_pwn fd 7322 Jun 11 2014 fd
-rw-r--r-- 1 root root 418 Jun 11 2014 fd.c
-r--r----- 1 fd_pwn root 50 Jun 11 2014 flag

我們需要 flag 的內容,但我們只是 fd 因此只能執行 fd 檔案。

1
2
fd@ubuntu:~$ whoami
fd

題目將原始碼附上了,查看有沒有可以使用的地方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf[32];
int main(int argc, char* argv[], char* envp[]){
if(argc<2){
printf("pass argv[1] a number\n");
return 0;
}
int fd = atoi( argv[1] ) - 0x1234;
int len = 0;
len = read(fd, buf, 32);
if(!strcmp("LETMEWIN\n", buf)){
printf("good job :)\n");
system("/bin/cat flag");
exit(0);
}
printf("learn about Linux file IO\n");
return 0;
}

首先看到得到 flag 的地方,在這邊必須讓 bufLETMEWIN\n" 相等。

1
2
3
4
5
if(!strcmp("LETMEWIN\n", buf)){
printf("good job :)\n");
system("/bin/cat flag");
exit(0);
}

查看 read 函式知道可以將檔案寫入 buf 中。

因此可以將 File descriptor 設為 0 ,則可以將使用者輸入寫入 buf。

接著看到 fd 的值相當於執行程式的參數減去 0x1234 (4660)

1
int fd = atoi( argv[1] ) - 0x1234;

因此首先執行 fd 且帶入 4660 參數 。

1
./fd 4660

此時會等待使用者輸入,這時輸入 LETMEWIN 即可得到 flag 。

1
2
3
LETMEWIN
good job :)
mommy! I think I know what a file descriptor is!!

flag 就是 mommy! I think I know what a file descriptor is!!